var course = new liwe.module ( "course", { 'pp_response' : '_pp_response', 'course_show' : '_show' } );
course.cbacks = { 'pp_after' :  null, 
		  'show' : null,
		  'after_set_user_form': null,
		  'check_registration' : null 
		};

course.dest_div = "block_main";

liwe.types.to_dict ( course.cbacks );

course.list_instances = {};
//{{{CourseListBox = function ( name, how_many, tag, dest_div )
CourseListBox = function ( name, how_many, tag, incoming )
{
	var self = this;
	this.name =  name;
	this.how_many = how_many; 
	this.tag = tag;
	this.incoming = incoming;
	this.ds = new DataSet ( 'coursebox:' + name );
	this.ds.templates [ 'table_start' ]  = course.templates [ 'COURSE-LIST-TB-START' ];
	this.ds.templates [ 'table_header' ]  = course.templates [ 'COURSE-LIST-HEADER'];
	this.ds.templates [ 'table_row' ] = course.templates [ 'COURSE-LIST-ROW' ];
	this.ds.templates [ 'table_end' ] = course.templates [ 'COURSE-LIST-TB-END' ];
	this.ds.templates [ 'table_footer' ] = course.templates [ 'COURSE-LIST-FOOTER' ];


	//{{{this._ds_rmanip = function ( ds, row)
	this._ds_rmanip = function ( ds, row)
	{
		row [ '_id' ] = row [ 'id' ];
		row [ '_how_many' ] = self.how_many;
		row [ '_HREF_CLICK' ] =  String.formatDict ( course.templates [ '_HREF_CLICK' ], row );
		row [ '_ACTION_CLICK'] = String.formatDict ( course.templates [ '_ACTION_CLICK' ], row );
		row [ '_link' ] = String.formatDict ( course.templates [ 'COURSE-LIST-LNK' ],  row );
	};
	//}}}
        this.ds.cbacks [ 'row_manip' ] = this._ds_rmanip;
	this.ds.lines_per_page = how_many;
	//{{{this.render = function ()
	this.render = function ( dest_div )
	{
		var _post = { 'action' : 'course.ajax.list_course', 
					  'limit' : self.how_many, 
					  'tags' : self.tag, 
					  'visible' : 1,
					  'incoming' : self.incoming }
	        self.ds.set_fields ( _post );
		self.ds.fill ( function () 
		{
	        	self.ds.render ( dest_div );
		});
	};
	//}}}	
};
//}}}


//{{{COURSE MODULE FUNCTION 
course._how_many = 10;
course._active_step_label = null;
course._active_step = null;
course._register_div = null;
course._reg_info = {};
course._odd_even = "odd";


//{{{course.init = function ()
course.init = function ()
{
	course.valid_events ( [ 'nopayment', 'before_show', 'render', 'place-order' ,'stat-show', 'stat-register', 'adwords-confirmed', "register_pick", "register_address", "register_confirm", "rester_end" ] );

	//user.events [ 'registered' ] = course._registered; 
	address_book.addEventListener ( 'noprofile', course._add_profile );

	
	user.addEventListener ( 'profile', course.user_profile );
	course.addEventListener ( 'render', function ( e )
	{
		var p = e.params;
		$el ( course.dest_div ).innerHTML = String.formatDict ( p.template, p.course );
		course._show_calendar ( 'course_calendar', p.id_course, p.how_many, p.course );
	}, true );

	course.addEventListener ( 'stat-show', function ( e ) 
	{
		liwe.ga.event ( 'Course', 'Show', e.params.name );
	} , true );

	course.addEventListener ( 'stat-register', function ( e )
	{
		liwe.ga.event ( "Course", "Register" , e.params.start_date )
	}, true );	
	course.addEventListener ( 'adwords-confirmed', function ( evt )
	{
		if ( ! evt.params.url ) return;
		var url = evt.params.url.indexOf ("http://") ? String.format("http://%s", evt.params.url ): evt.params.url ;

		if ( evt.params.new_page ) 
		{
			window.open( url , '_blank' )
			return;
		}
		window.location.href = url;
		window.location.assign ( url );
	}, true );
	course.addEventListener ( "place-order", function ( evt )
	{
		var reg_info = evt.params;
		//LA MAIL VA MANDATA SOLO A PAGAMENTO AVENUTO O SE E' UN CORSO GRATIS
		
		if ( ! course._reg_info.gratis() )
		{
			
			//ADD PRODUCT TO SHOPPING CART
			shop.add_product ( reg_info [ 'product_code' ], 1 );
			course.ajax ( { "action" : "product.ajax.coupon_add", "name" :  reg_info [ 'product_coupon' ]  } , function ( v )
			{
				shop.update_cart ( function ( k )
				{ 
					shop.update ();
					course.order_placed = true;
					//ADD ITEM ON SESSION
					var post = {	'action' : 'course.ajax.session_set_item',
							'key' : reg_info [ 'product_code' ],
							'coupon' : reg_info [ 'product_coupon' ],
							'id_calendar' : reg_info [ 'id_calendar' ],
							'id_course' :  reg_info [ 'id_course' ] };

					course.ajax ( post );
				} );
			} ); 

		} else {
			var post =  { 'action' : 'course.ajax.set_user',
					 'id_calendar' : reg_info [ 'id_calendar' ],
					 'id_course' :  reg_info [ 'id_course' ],
					 'note' :	'',
					 'send_mail' : 1
					};

			course.ajax ( post, function ( v )
			{
				course.transaction_completed();
			} );
		}
		
	}, true );

	function _clear_order ( evt )
	{
		course.order_placed = false;
		course.ajax ( { 'action' : "course.ajax.session_del_all" } );
	}

	shop.addEventListener ( "clear", _clear_order ); 
	shop.addEventListener ( "cart_empty", _clear_order );

	shop.addEventListener ( "cart_del", function ( evt )
	{
		var p = evt.params;
		course.ajax ( {"action" : "course.ajax.session_del_item" , 'key' : p.prod_code } );		 
	} );

	shop.addEventListener ( "complete_order", function ( evt ) {
		if (! course.order_placed ) return;
		
		var vals = { 'id_course': course._reg_info [ 'id_course' ],
			 'id_calendar' : course._reg_info [ 'id_calendar' ],
			 'uid' : course._reg_info [ 'profile' ] [ 'uid' ] ,
			 'cback' : 'course.ajax.paypal_cback'
			 };

		transaction.pp_custom_add ( vals );
	} );
	
	if ( ! course.templates )
	{
		course.load_templates ( function () 
		{
			course._init ();
		} );
		return;
	}
	
};
//}}}

//{{{course._init = function ()
course._init = function ()
{
	var ds  = new DataSet ( 'calendar_list' );
        ds.templates [ 'table_start' ]  = course.templates [ 'CALENDAR-LIST-TB-START' ];
        ds.templates [ 'table_header' ]  = course.templates [ 'CALENDAR-LIST-HEADER'];
        ds.templates [ 'table_row' ] = course.templates [ 'CALENDAR-LIST-ROW' ];
        ds.templates [ 'table_end' ] = course.templates [ 'CALENDAR-LIST-TB-END' ];
        ds.templates [ 'table_footer' ] = course.templates [ 'CALENDAR-LIST-FOOTER' ];
	ds.templates [ 'no_rows' ] = course.templates [ 'CALENDAR-LIST-NO-ROWS' ];
	ds.cbacks [ 'row_manip' ] = course._calendar_rmanip;
	course.ds_calendar =  ds;
		
};
//}}}
course._add_profile = function ( evt )
{
	var data = evt.params;
	if ( data [ 'module' ] == 'course' )
		address_book.edit_profile  ( "user-reg-info", data )
    return;
};
//{{{course._calendar_rmanip = function ( ds, row )
course._calendar_rmanip = function ( ds, row )
{ 
	row [ '_id' ] = row [ 'id' ];
	row [ "_start_date" ] = Date.formatted ( "%A %d %B %Y", row [ 'start_date' ].split( " ") [ 0 ]) ; 
	row [ "_tutor" ] = row [ 'trainer_login' ] ;
	row [ '_HREF_REGISTER' ] =  String.formatDict ( course.templates [ '_HREF_REGISTER' ], row );
	row [ '_ACTION_REGISTER'] = String.formatDict ( course.templates [ '_ACTION_REGISTER' ], row );
	row [ '_register' ] = String.formatDict ( course.templates [ 'REGISTER-LNK' ],  row );

	var dct = { '_price_vat' : Money.fromLongInt ( row [ 'price_vat' ] ),
		    '_max_value' : Money.fromLongInt ( row [ 'max_end_value' ] )
		   }

	row [ '_price' ] = String.formatDict ( course.templates [ 'CALENDAR-DISCOUNT' ], dct );
	if ( row [ 'max_end_value' ] == row [ 'price_vat' ] ) row [ '_price' ] = String.formatDict ( course.templates [ 'CALENDAR-END-VALUE' ] , dct );
	if ( row [ 'price_vat' ] == 0 ) row [ '_price' ] = course.templates [ 'CALENDAR-GRATIS' ] ;

	
	course._odd_even == "odd" ? course._odd_even = "even" : course._odd_even = "odd";
	row [ 'odd_even' ] = course._odd_even;
};
//}}}
course._show = function ( data )
{
	course.dispatchEvent ( "before_show", null, { 'id_course' : data [ 'id_course' ] , 'how_many' : data [ 'how_many' ] } );
	course.show ( data [ 'id_course' ], data [ 'how_many' ] );
}
//{{{course.show = function ( dest_div, id_course, how_many )
course.show = function ( id_course, how_many )
{

	// if ( typeof ( dest ) ) dest =  course.dest_div;
	if ( typeof ( how_many ) != 'undefined' ) course._how_many =  how_many;

	course.set_history ( "course_show", course._show, { "id_course" : id_course, "how_many" : how_many } );

	var post = { 'action' : 'course.ajax.get_course', 'id_course' : id_course };
	course.ajax ( post, function ( resp )
	{
		liwe.types.to_dict ( resp.course );
		course.dispatchEvent ( "render", null, { 'course' : resp.course, id_course : id_course, how_many : how_many, 'template' : course.templates [ 'SHOW-LAYOUT' ] } );
		course.dispatchEvent ( "stat-show", null , resp.course );

	});

};

//}}}
//{{{course._show_calendar = function ( dest_div, id_course, how_many )
course._show_calendar = function ( dest_div, id_course, how_many, data )
{
	if ( typeof ( how_many ) != 'undefined' ) course._how_many =  how_many;
	var _post = { 'action' : 'course.ajax.list_calendar', 'limit' : how_many, 'id_course' : id_course , mode : 'valid' } ;
	course.ds_calendar.set_fields ( _post );
	course.ds_calendar.fill ( function ()
	{
		course.ds_calendar.render ( dest_div );
		if ( course.cbacks.get ( 'show' ) )  course.cbacks.get ( 'show' ) ( data );
	});
};
//}}}
//{{{course._show_step = function ( step )
course._show_step = function ( step )
{
	if ( $el ( 'course_registration_wiz' ) == undefined ) return;
	if ( course._active_step_label ) liwe.dom.del_class ( course._active_step_label, 'active' );	
 
	course._active_step_label = $el ( 'step_' + step );
	liwe.dom.add_class ( course._active_step_label, 'active' );

	if ( course._active_step )
	{
		liwe.fx.fade_out ( course._active_step );
		course._active_step.style.display = "none";
	}	
	
	var _el = $el ( 'course_step_lay_' + step );
	_el.style.display = "block";
	liwe.fx.fade_in ( _el );
	course._active_step =  _el;
};
//}}}
//{{{course.register = function ( dest_div, id_calendar )
course.register = function ( dest_div, id_calendar )
{
	var _div = $el ( dest_div );
	if ( typeof ( address_book ) == 'undefined' ) 
	{
		$ ( _div, String.formatDict ( course.templates [ 'REGISTRATION-NO-ADDRESS_BOOK' ] ) );
		return;
	};
	var _post = { 'action' : 'course.ajax.show_register' , 'id_calendar' : id_calendar }
	course.ajax ( _post , function ( resp )
	{
		var _course =  dict ( resp.course  ); 
		_course [ "_start_date" ] = Date.formatted ( "%A %d %B %Y", _course [ 'start_date' ].split( " ") [ 0 ]) ; 
		_course [ '_how_many' ] = course._how_many;
		_course [ '_HREF_CLICK' ] =  String.formatDict ( course.templates [ '_HREF_CLICK' ], _course );
		_course [ '_ACTION_CLICK'] = String.formatDict ( course.templates [ '_ACTION_CLICK' ], _course );
		
		_course [ '_price_vat' ] = Money.fromLongInt ( _course [ 'price_vat' ] ) ;
		_course [ '_price' ] = String.formatDict ( course.templates [ 'REGISTRATION-PRICE' ], _course );

		if ( _course [ 'price_vat' ] == 0 ) _course [ '_price' ] = course.templates [ 'REGISTRATION-GRATIS' ];

		//CHECK OVERBOOKING
		_course [ '_overbook' ] = ""
		if ( _course.get ( 'members', 0 ) > _course [ 'places' ] ) 
		{
			_course [ '_overbook' ] = String.formatDict ( course.templates [ 'REGISTRATION-OVERBOOK' ], _course );
		}

		_course [ '_step_01' ] = String.formatDict ( course.templates [ 'REGISTRATION-STEP-01' ] , _course );

		$( _div, String.formatDict ( course.templates [ 'REGISTRATION-LAYOUT' ] , _course ) );
		course._register_div =  _div;
		course._show_step ( 1 );
		course._reg_info =  _course;	
		course._reg_info.gratis = function ()
		{
			var price = this [ 'price_vat' ];
			if ( typeof ( price ) == 'undefined' ) return false;
			price =  parseInt ( price, 10 );
			if ( price > 0 ) return false;
			return true;
		};
		course.dispatchEvent ( "stat-register", null , _course );
		course.dispatchEvent ( "register_pick", null , _course );
	});
};
//}}}
//{{{course.register_user = function ()
course.register_user = function ( dest_div )
{
	course.ajax ( { 'action' : 'course.ajax.ready_to_checkout' }, function ( res )
	{
		var _status = res [ 'status' ];
		switch ( _status )
		{
			case  "login":
				$( 'course_step_lay_2', String.formatDict ( course.templates [ 'REGISTRATION-STEP-02' ] , course._reg_info ) );
				user.render_login( dest_div , true, "course" );
				course._show_step ( 2 );
				break;
			case "profile":
				$( 'course_step_lay_2', String.formatDict ( course.templates [ 'REGISTRATION-STEP-02' ] , course._reg_info ) );
				address_book.render_profile ( "user-reg-info", "course", course._reg_info.gratis() );
				course.dispatchEvent ( "register_address", null ,{} );
				course._show_step ( 2 );
				break;
			case "address":
				course._reg_info [ 'profile' ] = res [ 'profile' ];
				$( 'course_step_lay_2', String.formatDict ( course.templates [ 'REGISTRATION-STEP-02' ] , course._reg_info ) );
				if ( ! course._reg_info [ 'price_vat' ] )
				{
					address_book.close ( 'course' );
					return;
				}
				address_book.render_address ( 'user-reg-info', 'course', res [ 'profile' ] );	
				course._show_step ( 2 );
				break;
			default:
				course._reg_info [ 'profile' ] = res [ 'profile' ];
				course._confirm_data ();
				break;
			
		};
	} );

};
//}}}
//{{{course._register_info = function ()
course._register_info = function ()
{
	address_book.render_profile ( "user-reg-info", "course", course._reg_info.gratis() );
//	$el ( 'user-lightbox').style.display = "none";
};
//}}}
//{{{course.user_profile = function ( evt )
course.user_profile = function ( evt )
{
	var post =  { 'action' : 'course.ajax.get_info' };
	course.ajax ( post , function ( res )
	{
		if ( res [ 'tot_rows' ] == 0 ) return;
		var buf = new String.buffer()
		buf.add ( course.templates [ 'SITE_PROFILE_INIT' ] );
		var _courses = to_list ( res [ 'rows' ] );
		_courses.iterate ( function ( c )
		{
			c [ '_start_date' ] =  Date.formatted ( "%A %d %B %Y", c [ 'start_date' ].split( " ") [ 0 ]) 
			c [ '_subscription_date' ] =  Date.formatted ( "%A %d %B %Y", c [ 'subscription_date' ].split( " ") [ 0 ]) 

			if ( c [ 'pay' ] == 0 ) 
			{
				c [ '_pay' ]  = "Gratuito";
				c [ '_paid' ]  = "";
			}else{
				c [ '_pay' ] = String.formatDict ( "&euro; %(pay)m" , c );
				c [ '_paid' ] = String.formatDict ( "&euro; %(paid)m" , c );
			}

			buf.add ( String.formatDict ( course.templates [ 'SITE_PROFILE_ROW' ], c ) );
		} );
		buf.add ( course.templates [ 'SITE_PROFILE_END' ] );

		user.profile_add_box ( buf.toString() , "course" );
	} );
}
//}}}
//{{{course.set_profile_address = function ( evt )
course.set_profile_address = function ( evt )
{

	var p = evt.params;
	course._reg_info [ 'profile' ] = p;
	if ( ! course._reg_info [ 'price_vat' ] ) return address_book.close( 'course' );
	address_book.render_address ( 'user-reg-info', 'course' );	
	//$el ( 'user-lightbox').style.display = "none";
}
//}}}
//{{{course._confirm_data = function ()
course._confirm_data = function ()
{

	$( 'user-reg-info', '' );
	var _post = {	'action' : 'course.ajax.get_user_charge', 
					id_course : course._reg_info [ 'id_course' ],
					id_calendar : course._reg_info [ 'id_calendar' ] };
	/*
	course.ajax ( _post , function ( _resp )
	{
	*/
	var total_tpl = "";
	var pay_select_tpl = "";
	course._reg_info [ '_price_vat' ] = 0;
	if ( course._reg_info [ 'value' ] )	
	{
		course._reg_info [ '_value' ] =  course._reg_info [ 'value' ];
		course._reg_info [ '_payment' ] = 0;
		course._calc_totals ();
		total_tpl = String.formatDict ( course.templates [ '_TOTALS' ], course._reg_info );
		pay_select_tpl = course.templates [ '_PAY_SELECT' ];

	}	

	var _profile = course._reg_info [ 'profile' ];
	_profile [ '_complete_name' ] = _profile [ 'name' ] + ' ' + _profile [ 'lastname' ] ;

	var user_tpl = '_USERDATA';
	if ( course._reg_info.gratis() ) user_tpl = '_USERDATA_MINIFY';

	var _dct = { '_COURSEDATA' : String.formatDict ( course.templates [ '_COURSEDATA' ], course._reg_info ),
		     '_USERDATA' : String.formatDict ( course.templates [ user_tpl ] , course._reg_info [ 'profile' ] ),
		     '_TOTALS' : total_tpl,
  		     '_PAY_SELECT' : pay_select_tpl };
	
	$( 'course_step_lay_3', String.formatDict ( course.templates [ 'REGISTRATION-STEP-03' ], _dct ) );

	address_book.address_box ( 'address_fatt', 0, "Indirizzo di Fattuazione" , "course" );

				
	if ( course._reg_info [ 'value' ] )	
	{
		transaction.render_transaction_list ( 'course_pay_kind_select', 'course' );
	};
	course._show_step ( 3 );
	/*
	});
	*/
};
//}}}
//{{{course.render_payment_list = function ()
course.render_payment_list = function ()
{
	$( 'course_pay_kind' , "" );
	transaction.render_transaction_list ( 'course_pay_kind_select', 'course' );
};
//}}}
//{{{course._calc_totals = function ()
course._calc_totals = function ()
{
	course._reg_info [ '_taxable' ] =  course._reg_info [ '_value' ] + course._reg_info [ '_payment' ];
	var tax = 1 + ( course._reg_info [ 'tax_code']  / 100 );
	course._reg_info [ '_price_vat' ] = Math.round (  course._reg_info [ '_taxable' ] * tax );
	course._reg_info [ '_tax' ] =  course._reg_info [ '_price_vat' ] - course._reg_info [ '_taxable' ];
	//STRING PRICE
	course._reg_info [ '_str_payment' ] = Money.fromLongInt ( course._reg_info [ '_payment' ] ) ;
	course._reg_info [ '_str_taxable' ] = Money.fromLongInt ( course._reg_info [ '_taxable' ] ) ;
	course._reg_info [ '_str_tax' ] = Money.fromLongInt ( course._reg_info [ '_tax' ] ) ;
	course._reg_info [ '_str_price_vat' ] = Money.fromLongInt ( course._reg_info [ '_price_vat' ] ) ;
};
//}}}
//{{{course.set_transaction = function ( evt )
course.set_transaction = function ( evt )
{
    var p = evt.params;
	$( 'course_pay_kind_select' , "" );
	$ ( 'course_pay_kind' , String.formatDict ( course.templates [ '_PAYMENT' ] , p.kind ) );
	course._reg_info [ 'payment' ] = p.kind;
		
	course._reg_info [ '_payment' ] = p.kind [ 'charge' ];
	course._calc_totals ();
	$( 'course_total', String.formatDict ( course.templates [ '_TOTALS' ] , course._reg_info ) );
};
//}}}
//{{{course.set_address = function ( kind )
course.set_address = function ( kind )
{
	address_book.select_address_box ( "address_select", kind, 'course' );
};
//}}}
//{{{course.edit_profile =  function ( evt )
course.edit_profile =  function ( evt )
{
    var p = evt.params;
    if ( $( "user-reg-info" ) )
    {
        course_book.edit_profile ( "user-reg-info", p );
    };
}
//}}}
//{{{course._registered = function ( vals )
course._registered = function ( vals )
{
	address_book.get_location_opts ( function () {  course._register_form ( vals ); }, null, vals );
	$el ( 'user-lightbox').style.display = "none";
};
//}}}
//{{{course.set_user_course_address =  function ( evt )
course.set_user_course_address =  function ( evt )
{
    var p = evt.params;
	address_book.address_box ( 'address_fatt', p [ 'id_address' ], "Indirizzo di Fattuazione" , "course" );
	$( "address_select", "" );	
}
//}}}
//}}}
//{{{course.register_payment = function ()
course.register_payment = function ()
{
	course.dispatchEvent ( "adwords-confirmed", null, course._reg_info );
	course.dispatchEvent ( "place-order", null, course._reg_info );
};
//}}}
//{{{course.transaction_completed = function ( evt )
course.transaction_completed = function ( evt )
{
	var dct = { 'user_name' : course._reg_info [ 'profile' ] [ 'name' ],
				'user_lastname' : course._reg_info [ 'profile' ] [ 'lastname' ],
				'course_name' : course._reg_info [ 'name' ]
			  };
	$( 'course_step_lay_4',  String.formatDict ( course.templates [ 'REGISTRATION-STEP-04' ], dct ) );
	course._show_step ( 4 );
};
//}}}
//{{{course._check_user_course = function ( id_calendar, cback )
course._check_user_course = function ( id_calendar, cback )
{

	if ( ( typeof ( id_calendar ) == 'undefined' ) || id_calendar == null )  id_calendar =  course._reg_info [ 'id_calendar' ];
	var post = { 'action' : 'course.ajax.get_info' } ;
	course.ajax ( post, function ( resp )
	{
		var res = false;
		if ( resp [ 'tot_rows' ] == 0 ) 
		{ 
			cback ( res );
			return;
		}
		var _courses = to_list ( resp [ 'rows' ] );
		_courses.iterate ( function ( c )
		{
			if ( c [ 'id_calendar' ] == id_calendar ) res = true;
		} );
		cback ( res );
	} );
};
//}}}
//{{{course.plugin = function ( div, params )
course.plugin = function ( div, params )
{
	switch ( params._type )
	{
		case "list" :
			var _list_box = course.list_instances.get ( params [ 'tag' ], false ) ;
			if ( ! _list_box )
			{
				_list_box = new CourseListBox ( params [ 'tag' ], params [ 'how_many' ], params  [ 'tag' ], params [ 'incoming' ] );
				course.list_instances [ params [ 'tag' ] ] = _list_box;
			}
			_list_box.render( div );
			break;
		case "incoming" :
			course._render_incoming ( div, params );	
			break;	
	};
};
//}}}
course._render_incoming  = function( dest_div, p )
{
	var buf = new String.buffer();
	buf.add ( course.templates [ 'SITE_INCOMING_START' ] ); 
	var post = { "action" : "course.ajax.incoming_dates" , "how_many" : p.how_many };
	course.ajax ( post, function ( resp )
	{
		resp = dict ( resp )
		var dates =  list ( resp.get ( 'upcoming', [] ) )
		dates.iterate ( function ( date )
		{
			var date = dict ( date );
			date [ '_start_date' ] = Date.formatted ( p.date_format, date [ 'start_date' ].split( " ") [ 0 ]); 

			date [ '_price_vat' ] = "";
			if ( date.get ( 'price_vat', 0 ) !=0 )
				date [ '_price_vat' ] = String.format ("%m &euro;",  date.get ( 'price_vat', 0 ) ) ;

			date [ '_gratis' ] = "&nbsp;"
			if ( date.get ( 'price_vat', 0 )  == 0 ) date [ '_gratis' ] = course.templates [ 'SITE_INCOMING_GRATIS' ];
			buf.add ( String.formatDict ( course.templates [ 'SITE_INCOMING_ROW' ], date ) );
		} );
		buf.add ( course.templates [ 'SITE_INCOMING_END' ] );
		$( dest_div, buf.toString() );
	} );
}


//{{{UNIT-TEST
//{{{course.test_static_page = function ( dest_div, page, db )
course.test_static_page = function ( dest_div, page, db )
{
        staticpage.get_page ( page, db, function ( p ) {
		var _dest_div = $el ( dest_div );
		_dest_div.innerHTML = '';
		var _title = document.createElement ( 'h1' );
		_title.id = 'static_page_title';
		var _text = document.createElement ( 'div' );
		_text.id = 'static_page_text';
		_dest_div.appendChild ( _title );
		_dest_div.appendChild ( _text );
		
                $ ( 'static_page_text' ).innerHTML = p [ 'html' ];
                $ ( 'static_page_title' ).innerHTML = p [ 'title' ];
		// system.plugins_render()
        } );
};
//}}}
//}}}




//{{{course._register_form = function ( data ) OBSOLETA
/*
course._register_form = function ( data )
{
	if ( typeof ( data ) == 'undefined' ) data = {};
	
	liwe.types.to_dict ( data );
	
	f =  new liwe.form.instance ( 'course-create-profile' );

	f.hidden ( 'uid', data.get ( 'uid', 0 )  );
	f.hidden ( 'owner', data.get ( 'uid', 0 )  );
	f.hidden ( 'email', data.get ( 'user_mail', '' )  );

	f.label ( { value: "Dati anagrafici" } );
	f.text ( { 'label' : 'Nome', 'name' : 'name', size : 30, 'maxlength' : 30, mandatory : true,  value : data.get ( 'name' , '' ) } );
	f.text ( { 'label' : 'Cognome', 'name' : 'lastname', size : 30, 'maxlength' : 30, mandatory : true,  value : data.get ( 'lastname' , '' )} );

	f.label ( { value: "Indirizzo" } );
	f.select ( { 'label' : 'Stato', 
                 'name' : 'country', 
                 'options' : address_book._loc_opts [ 'stati' ], 
                 force_select : true , 
                 onchange: "address_book.change_stato( this, course._register_form, 'course-create-profile' )", 
                 value : data.get ( 'country' , 'Italia' )} );
	if ( data.get ( 'country', 'Italia' ) == 'Italia' )
	{
	        f.select ( { 'label' : 'Provincia', 
        	             'name' : 'prov', 'options' : address_book._loc_opts  [ 'province' ], 
			     onchange: "address_book.change_prov( this, course._register_form, 'course-create-profile' )", 
	                     force_select : true ,  
        	             value : data.get ( 'prov' , '' )} );

	        if ( data.get ( 'prov' ) )
	        {
        		f.select ( { name: "city", label: "Citt&agrave;", 
				     mandatory: true, options: address_book._loc_opts [ data.prov ], 
				     force_select: true, 
				     onchange: "address_book.change_comu( this, course._register_form, 'course-create-profile' )" , 
				     value: data.get ( 'city', '' ) } );
		};
		//f.text ( { 'label' : "Citt&agrave;", 'name' : 'city' , size : 30, 'maxlength' : 30, value : data.get ( 'city', '' ) } );
		if ( data.get ( 'city' ) )
		{
			f.select ( { name: "zip_code", label: "C.A.P.", mandatory: true, options: address_book._loc_opts [ data.prov + data.city ], value: data.get ( 'zip_code', '' ) } );
		};
		f.text ( { 'label' : 'Indirizzo', 'name' : 'addr1' , 'size' : 30 , 'maxlength' : 100, 'colspan' : 5, value : data.get ( 'addr1', '' ) } );
	} else {
		f.textarea ( { 'label' : 'Indirizzo', 'name' : 'addr1' , 'rows' : 5 , 'cols' : 50, value : data.get ( 'addr1', '' ) } );
	};
	
	f.label ( { value: "Telefono e-mail" } );
	f.text ( { 'label' : "Telefono", 'name' : 'phone' , size : 30, 'maxlength' : 30,  value : data.get ( 'phone', '' ) } );
	f.label ( { 'label' : 'e-mail', value : data.get ( 'email') } );
	f.label ( { value: " Emissione di fattura per i titolari di Partita IVA" } );
	f.cod_fisc ( { 'label' : 'Cod. Fiscale', 'name' : 'tax_code', size : 30, 'maxlength' : 20, mandatory : true, value : data.get ( 'tax_code' , '' ) } );
	f.p_iva ( { 'label' : 'P.IVA', 'name' : 'vat_code', size : 30, 'maxlength' : 20, value : data.get ( 'vat_code' , '' ) } );
	f.textarea ( { 'label' : 'Note', 'name' : "note", cols: 70, rows: 5, colspan : 5, value : data.get ( 'note' , '' ) } );
	f.sep();
        f.button ( { value: 'Salva', 'class' : "button green", onclick : course._register_save_profile } );
	f.set ( 'user-reg-info' );
	if ( course.cbacks [ 'after_set_user_form' ] ) course.cbacks [ 'after_set_user_form' ] ();
	
};
*/
//}}}
//{{{course._register_save_profile = function () OBSOLETA
/*
course._register_save_profile = function ()
{
	if ( course.cbacks [ 'check_registration' ] )
	{
		if ( !course.cbacks [ 'check_registration' ]() ) return;
	}

	var f = liwe.form.get ( 'course-create-profile' );
	if ( ! f.check() ) return;
	var post = f.get_values();
	post [ 'action' ] = 'address_book.ajax.set_profile' ;
	address_book.ajax ( post , function ( resp )
	{
		course._reg_info [ 'profile' ] = resp.profile;
		course._confirm_data ()		
	});
};
*/
//}}}
//{{{/ OLD CODE
/*
	if ( course._reg_info [ 'price_vat' ] == 0 )
	{
		var post = { 'action' : 'course.ajax.set_user',
					 'uid' : course._reg_info [ 'profile' ] [ 'uid' ],
					 'id_calendar' : course._reg_info [ 'id_calendar' ],
					 'id_course' :  course._reg_info [ 'id_course' ],
					 'pay' : course._reg_info [ 'price_vat' ],
					 'note' : '',
					 'send_mail' : 'GG'
					};
		course.ajax ( post, function ( resp )
		{
			var resp = { 'esito': 1 };
			if ( course.cbacks [ 'pp_after' ] ) course.cbacks [ 'pp_after' ] ( resp );
			return;
		} );
	}  else {
		$( 'course_step_lay_4',  course.templates [ 'REGISTRATION-STEP-04' ] );
		course._show_step ( 4 );
	}
}

course.commit_payment = function ()
{
    var _f = $el ('payment_form' );
    var tipo_pagamento= "";
    for ( i = 0; i < _f.tipo.length; ++i )
    {
        if ( _f.tipo [ i ].checked )
        {
            tipo_pagamento = _f.tipo [ i ].value;
            break;
        }
    };
    if ( tipo_pagamento == "" )
    {
        liwe.utils.notifier.show ( 'Selezionare un tipo di pagamento per proseguire', 'info', 5000);
        return;
    }
    if ( tipo_pagamento == 'PP' )
    {
            var prods = [ { 'product_code' : course._reg_info [ 'name' ] ,
                            'descr': course._reg_info [ 'descrizione_corso' ] ,
                            'quant' : 1,
                            'price_vat' : course._reg_info [ 'price_vat' ]  } ];
            var sped = { 'total_vat' :  0  };
            var vals = { 'id_course': course._reg_info [ 'id_course' ],
			 'id_calendar' : course._reg_info [ 'id_calendar' ],
			 'uid' : course._reg_info [ 'profile' ] [ 'uid' ] ,
			 'cback' : 'course.ajax.paypal_cback',
			 'module' : 'course'
		 	 };
            transaction.send_paypal ( vals, prods, sped )
    };

    if ( tipo_pagamento == 'BB' )
    {
		var post = { 'action' : 'course.ajax.set_user',
					 'uid' : course._reg_info [ 'profile' ] [ 'uid' ],
					 'id_calendar' : course._reg_info [ 'id_calendar' ],
					 'id_course' :	course._reg_info [ 'id_course' ],
					 'pay' : course._reg_info [ 'price_vat' ],
					 'note' : 'Bonifico da verificare',
					 'send_mail' : 'BB'
					};
		course.ajax ( post, function ( resp )
		{
			var resp = { 'esito': 1 };
			if ( course.cbacks [ 'pp_after' ] ) course.cbacks [ 'pp_after' ] ( resp );
		} );
    };
};
*/
//}}}
